export function jsonToTableOptions(
  widgetList,
  config = {
    sort: [],
    sortable: [],
  },
  isNew = false
) {
  let tableList = [];
  widgetList.forEach((item) => {
    if (item.type === "slot" && !isNew) return;
    if (item.type === "static-text") return;
    if (item.type === "grid" || item.type === "table") {
      if (item.type === "table") {
        item.rows.forEach((col) => {
          col.cols.forEach((col) => {
            tableList = [
              ...tableList,
              ...jsonToTableOptions(col.widgetList, config, isNew),
            ];
          });
          // tableList = [
          //   ...tableList,
          //   ...jsonToTableOptions(col.widgetList, config, isNew),
          // ];
        });
        return;
      }
      item.cols.forEach((col) => {
        tableList = [
          ...tableList,
          ...jsonToTableOptions(col.widgetList, config, isNew),
        ];
      });
    } else {
      tableList.push({
        label: item.options.label,
        prop: item.options.name,
        sort:
          config.sort.findIndex((i) => i === item.options.name) === -1
            ? 99
            : config.sort.findIndex((i) => i === item.options.name),
        sortable: config.sortable.includes(item.options.name),
        type: item.type || item.options.type,
        optionItems: item.options.optionItems || [],
        valueFormat: item.options.valueFormat || null,
      });
    }
  });
  tableList = tableList.filter((item) => !item.prop.includes("remark"));
  return tableList.sort((a, b) => {
    return a.sort - b.sort;
  });
}
